<?php

namespace CNAM\ActimanBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use CNAM\ActimanBundle\Entity\Tache;
use CNAM\ActimanBundle\Form\TacheType;
use CNAM\ActimanBundle\Entity\ProcedurePlanifiee;
use CNAM\ActimanBundle\Form\ProcedurePlanifieeType;
use CNAM\ActimanBundle\Entity\phasePlanifiee;
use CNAM\ActimanBundle\Form\phasePlanifieeType;
use CNAM\ActimanBundle\Entity\acProcedure;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

class PlanProcedureController extends Controller {

    public function indexAction() {
        return $this->render('CNAMActimanBundle:Default:index.html.twig');
    }

    /* Affiche la vue d'un calendrier */

    public function visualizeProcedureAction() {
        $titre = "Visualiser une procédure";
        $data = -1;
        /* accés au repository */
        $repository = $this->getDoctrine()->getRepository('CNAMActimanBundle:acProcedure');
        $listeAcProcedure = $this->getDoctrine()->getRepository('CNAMActimanBundle:acProcedure')->findAll();
        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $data = $this->get('request')->request->get('filtre');
        }
        if ($data != -1)
            $events = $repository->findByAcProcedure($data);
        else
            $events = $repository->findAll();


        return $this->render('CNAMActimanBundle:Default:PlanProcedure:visualizeProcedure.html.twig', array('titre' => $titre, 'events' => $events));
    }

    public function listProceduresPlanifiablesAction() {
        $em = $this->getDoctrine()->getManager();
        $repository_acProcedure = $em->getRepository('CNAMActimanBundle:acProcedure');
        $list_procedure = $repository_acProcedure->findAll();


        return $this->render('CNAMActimanBundle:planProcedure:listProceduresPlanifiables.html.twig', array('list_procedure' => $list_procedure));
    }

    public function planifierProcedureAction($idpro) {


        $procedureplan = new ProcedurePlanifiee();

        $em = $this->getDoctrine()->getManager();

        $procedure = $this->getDoctrine()
                ->getRepository('CNAMActimanBundle:acProcedure')
                ->find($idpro);

        $procedureplan->setAcProcedure($procedure);
        $procedureplan->setDateDebutProc(new \Datetime());
        $procedureplan->setDateFinProc(new \Datetime());



        $form_proc = $this->createForm(new ProcedurePlanifieeType(), $procedureplan);

        $request = $this->get('request');
        if ($request->getMethod = 'POST') {

            $form_proc->bind($request);
        }
        if ($form_proc->isValid()) {
            if ($form_proc->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $em->persist($procedureplan);
                $em->flush();

                $list_phase = $procedure->getPhases();
                              
                
                
                foreach ($list_phase as $phase){
                    $phaseplan= new phasePlanifiee();
                    $phaseplan->setPhase($phase);
                    $phaseplan->setProcedurePlanifiee($procedureplan);
                    $em->persist($phaseplan);
                    $em->flush();
                    
                    $list_operation = $phase->getOperations();
                    
                    foreach ($list_operation as $operation){
                        $tache= new Tache();
                        $tache ->setNomTache($operation->getNomOperation());
                        $tache->setOperation($operation);
                        $tache->setphasePlanifiee($phaseplan);
                        $em->persist($tache);
                        $em->flush();
                    }
                       
                }
                
                $repository_phaseplan = $em->getRepository('CNAMActimanBundle:phasePlanifiee');
                $list_phaseplan = $repository_phaseplan->findBy(Array('ProcedurePlanifiee' => $procedureplan->getId()));
      
                $repository_tache = $em->getRepository('CNAMActimanBundle:Tache');
                $list_tache = $repository_tache->findAll();
                
                return $this->render('CNAMActimanBundle:PlanProcedure:VisuPlanifierProcedure.html.twig', array('procedureplan' => $procedureplan, "list_phaseplan" => $list_phaseplan, "list_tache"=>$list_tache));
            }
        }


        return $this->render('CNAMActimanBundle:planProcedure:planifierProcedure.html.twig', array('form_proc' => $form_proc->createView(), 'idpro' => $idpro, 'procedureplan' => $procedureplan));
    }

    public function VisuPlanifierProcedure() {
        return $this->render('CNAMActimanBundle:PlanProcedure:VisuPlanifierProcedure.html.twig');
    }

    public function planifierPhaseAction($idpha) {
      $em = $this->getDoctrine()->getManager();

      $phaseplan = $this->getDoctrine()
      ->getRepository('CNAMActimanBundle:phasePlanifiee')
      ->find($idpha);
      
      $phaseplan->setDateFinPhase(new \Datetime());
      $phaseplan->setDateFinPhase(new \Datetime());

      $form = $this->createForm(new phasePlanifieeType(), $phaseplan);

        $request = $this->get('request');
        if ($request->getMethod = 'POST') {

            $form->bind($request);
        }
        if ($form->isValid()) {
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $em->persist($phaseplan);
                $em->flush();              
                
            }
            $procedureplan = $phaseplan->getProcedurePlanifiee();
            $repository_phaseplan = $em->getRepository('CNAMActimanBundle:phasePlanifiee');
            $list_phaseplan = $repository_phaseplan->findBy(Array('ProcedurePlanifiee' => $procedureplan->getId()));
      
            $repository_tache = $em->getRepository('CNAMActimanBundle:Tache');
            $list_tache = $repository_tache->findAll();          
          
            return $this->render('CNAMActimanBundle:PlanProcedure:VisuPlanifierProcedure.html.twig', array('procedureplan' => $procedureplan, "list_phaseplan" => $list_phaseplan, "list_tache"=>$list_tache));
        }
      return $this->render('CNAMActimanBundle:PlanProcedure:planifierPhase.html.twig', array("phaseplan" => $phaseplan, 'form' => $form->createView()));
    }
    
    public function planifierTacheAction($idta) {
      $em = $this->getDoctrine()->getManager();

      $tache = $this->getDoctrine()
      ->getRepository('CNAMActimanBundle:Tache')
      ->find($idta);
      
      $tache->setDateDebutTheo(new \Datetime());
      $tache->setDateFinTheo(new \Datetime());

      $form = $this->createForm(new TacheType(), $tache);

        $request = $this->get('request');
        if ($request->getMethod = 'POST') {

            $form->bind($request);
        }
        if ($form->isValid()) {
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $em->persist($tache);
                $em->flush();
            }
        $phaseplan = $tache->getPhasePlanifiee();   
        $procedureplan = $phaseplan->getProcedurePlanifiee();
        $repository_phaseplan = $em->getRepository('CNAMActimanBundle:phasePlanifiee');
        $list_phaseplan = $repository_phaseplan->findBy(Array('ProcedurePlanifiee' => $procedureplan->getId()));
      
        $repository_tache = $em->getRepository('CNAMActimanBundle:Tache');
        $list_tache = $repository_tache->findAll(); 
        
        return $this->render('CNAMActimanBundle:PlanProcedure:VisuPlanifierProcedure.html.twig', array('procedureplan' => $procedureplan, "list_phaseplan" => $list_phaseplan, "list_tache"=>$list_tache));
        }
      return $this->render('CNAMActimanBundle:PlanProcedure:planifierTache.html.twig', array("tache" => $tache, 'form' => $form->createView()));
    }
    
    public function visuDetailAction($idpro) {
        $em = $this->getDoctrine()->getManager();

        $repository_procedureplanifiee = $em->getRepository('CNAMActimanBundle:ProcedurePlanifiee');
        $proc = $repository_procedureplanifiee->find($idpro);

        $repository_phaseplanifiee = $em->getRepository('CNAMActimanBundle:phasePlanifiee');
        $list_phase = $repository_phaseplanifiee->findBy(Array('ProcedurePlanifiee' => $idpro));

        $repository_tache = $em->getRepository('CNAMActimanBundle:Tache');
        $tabResult = array();
        $tabResult = null;
        $ligne = 0;
        foreach ($list_phase as $phase) {
            $tabResult[$ligne] = $phase;
            $ligne = $ligne + 1;
        }
        $list_tache = $repository_tache->findBy(Array('phasePlanifiee' => $tabResult));

        return $this->render('CNAMActimanBundle:PlanProcedure:visuDetail.html.twig', array('proc' => $proc, 'list_phase' => $list_phase, 'list_tache' => $list_tache));
    }

    public function listProcedurePlanAction() {
        $em = $this->getDoctrine()->getManager();

        $repository_ProcedurePlanifiee = $em->getRepository('CNAMActimanBundle:ProcedurePlanifiee');
        $list_procedureplan = $repository_ProcedurePlanifiee->findAll();

        return $this->render('CNAMActimanBundle:PlanProcedure:listProcedurePlan.html.twig', array('list_procedureplan' => $list_procedureplan));
    }

}
